package com.longxin.demoapp.base.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.longxin.demoapp.R;
import com.longxin.demoapp.base.service.IBaseService;

import java.util.List;

/**
 * @author Long
 * @version 1.0
 * @description: 复用Adapter
 * @remarks:
 * @title IBaseAdapter
 * @date 2021/4/7 12:01
 */
public abstract class  IBaseAdapter<T> extends BaseAdapter {
    //活动
    protected Activity activity;
    //数据
    protected List<T> datas;
    //布局加载器
    protected LayoutInflater inflater;
    //item中的布局
    protected ViewGroup layout;


    public IBaseAdapter(Activity activity, List<T> datas) {
        this.activity = activity;
        this.datas = datas;
        //得到布局加载器
        this.inflater = LayoutInflater.from(activity);
    }

    public void init(int resource){
        layout =  (ViewGroup) inflater.inflate(resource, null);
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void setDatas(List<T> datas) {
        if (datas != null) {
            this.datas.clear();
            this.datas.addAll(datas);
        }
        notifyDataSetChanged();
    }

    public void addDatas(T data) {
        if (data != null) {
            this.datas.add(data);
        }
        notifyDataSetChanged();
    }
}
